﻿using System;
using System.Collections.Generic;
using AbstractBugTracker;
using SmartAssembly;
using Utilities;

namespace Engine.Buggets
{
    internal sealed class Reopener<TBug> : IBugget<TBug>
        where TBug : class, IBug
    {
        public IEnumerable<Tuple<ProcessedReport, TBug>> Run(ProcessedReport processedReport, TBug bug)
        {
            BugState bugState = bug.State;

            if (bugState == BugState.BelievedFixed)
            {
                Version buildFixedOn = bug.BuildFixedOn;

                // Use the build fixed on if it's available
                if (buildFixedOn != null)
                {
                    Version buildVersion = processedReport.ProductBuildRef.ProductBuildVersion;
                    if (buildVersion >= buildFixedOn)
                    {
                        bug.Reopen();
                    }
                }
                else
                {
                    // No fix build is available, use dates instead
                    DateTime buildDateTime = processedReport.Report.BuildDateUtc;
                    DateTime resolvedDate = bug.ResolvedDateUtc;

                    if (buildDateTime > resolvedDate)
                    {
                        bug.Reopen();
                    }
                }
            }

            yield return new Tuple<ProcessedReport, TBug>(processedReport, bug);
        }
    }
}
